package com.jingdianjichi.subject.application.controller;

import com.jingdianjichi.subject.application.converter.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/*
 * 刷题controller
 *
 * */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /*
    * 新增题目
    * */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.add.dto:{}",subjectInfoDTO);
            }

            Objects.requireNonNull(subjectInfoDTO.getSubjectName(),
                    "题目名字不能为空");
            Objects.requireNonNull(subjectInfoDTO.getSubjectDifficult(),
                    "题目难度不能为空");
            Objects.requireNonNull(subjectInfoDTO.getSubjectAnswer(),
                    "题目答案不能为空");
            Objects.requireNonNull(subjectInfoDTO.getSubjectParse(),
                    "题目解析不能为空");
            Objects.requireNonNull(subjectInfoDTO.getSubjectType(),
                    "题目类型不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.
                    Instance.SubjectInfoDTOconverterBO(subjectInfoDTO);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add:{}",e.getMessage(),e);
            return Result.fail("新增题目失败");
        }
    }

    /*
    * 查询题目列表
    * */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPage.dto:{}",subjectInfoDTO);
            }

            Objects.requireNonNull(subjectInfoDTO.getCategoryId(),"题目的分类id不能为空");
            Objects.requireNonNull(subjectInfoDTO.getLabelId(),"题目的标签id不能为空");
            Objects.requireNonNull(subjectInfoDTO.getPageNo(),"当前的页码不能为空");
            Objects.requireNonNull(subjectInfoDTO.getPageSize(),"页码大小不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.
                    Instance.SubjectInfoDTOconverterBO(subjectInfoDTO);

//            log.info("subjectInfoBO:{}",subjectInfoBO);

            PageResult<SubjectInfoBO> pageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(pageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage:{}",e.getMessage(),e);
            return Result.fail("查询题目列表失败");
        }
    }

    /*
    * 查询题目详情
    * */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.querySubjectInfo.dto:{}",subjectInfoDTO);
            }

            Objects.requireNonNull(subjectInfoDTO.getId(),"题目的id不能为空");

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.
                    Instance.SubjectInfoDTOconverterBO(subjectInfoDTO);

            SubjectInfoBO bo = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO dto = SubjectInfoDTOConverter.
                    Instance.SubjectInfoBOconverterDTO(bo);
            return Result.ok(dto);
        } catch (Exception e) {
            log.error("SubjectController.querySubjectInfo:{}",e.getMessage(),e);
            return Result.fail("查询题目详情失败");
        }
    }
}
